#!/usr/bin/env python3

#  一.输出1到100之间数之和
def sumNum(endNum):#{
    s = 1
    i = 2
    while i <= endNum:
        s += i
        i += 1

    print("s = ", s)

    #  for实现
    s = 0
    for i in range(endNum + 1):
        s += i
    print("s = ", s)

    #  sum求和
    print("s = ", sum(range(endNum + 1)))
#}

#  二.用户输入一个年份判断是否为闰年(能被4整除但不能被100整除或能被400整除)
def isYear(year):#{
    #  if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
        #  return True
    #  else:
        #  return False

    #  if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
        #  return True
    #  return False

    return 1 if year % 400 == 0 or year % 4 == 0 and year % 100 != 0 else 0
#}

#  三.判断2000到2500年中有多少个闰年
def countIsYear():#{
    s = 2000
    count = 0

    while s <= 2500:
        #  if isYear(s):
            #  count += 1
        count += isYear(s)
        s += 1

    print("2000 到 2500年之间有", count, "个闰年", sep='')
#}

#  四.交换两个变量的值
def swapVar(): #{
    a = "3"
    b = "5"

    print("old a = " + a + ", b = " + b)

#  第三方变量实现
#  c = a
#  a = b
#  b = c

#  元组赋值实现
#  a, b = b, a

#  只能针对整型数据 实现异或交换
#  a ^= b
#  b ^= a
#  a ^= c

    print("a = " + a + ", b = " + b)
#}

#   五.计算输入一个数二进制中有多少个1 (32位)
def countBinOne(num): #{
    if num < 0:
        num *= -1

    i = 0
    count = 0
    while i < 32:
        if num & (1 << i):
            count += 1
        i += 1
    print(str(num) + " 二进制中有 " + str(count) + " 个1")
#}

#  六.输入一个数字, 对此数字二进制位第3 5 7置1, 将2 6位置0再输出(不考虑负数)
def bitOp(num): #{
#  把某位置1: num |= (1 << bit)
#  把某位置0: num &= ~(1 << bit)

#  原数字: 1000 0010
#  置1: num | 0000 0001
#  置0: num & 1111 1110
#  把第3 5 7位置1: num | 0101 0100
#  把第2 6位置0: num & 1101 1101


        #  1010 1010  170
        #  1101 1100  220

        #  二进制转十进制:
                #  1010
                 #  1*2**3 + 0*2**2 + 1*2**1 + 0*2**0
                 #  8 + 0 + 2 + 0 = 10
    num |= (1 << 2 | 1 << 4 | 1 << 6)
    num &= ~(1 << 1 | 1 << 5)

    print(num)
#}

#  七.一个袋子里有3个红球，3个绿球，6个黄球，一次从袋子里取6个球，列出所有可能组合
def ball():#{
    #  r = 0
    #  while r <= 3:
        #  g = 0
        #  while g <= 3:
            #  print("红球", r, ", 绿球", g,", 黄球", 6 - r - g, sep='')
            #  g += 1
        #  r += 1

    for r in range(4):
        for g in range(4):
            print("红球", r, ", 绿球", g,", 黄球", 6 - r - g, sep='')
#}

#  八.公鸡5元一只，母鸡3元一只，小鸡1元3只， 现有100元要买100只，列出所有可能组合
def buy():#{
    g = 0

    while g <= 20:
        m = 0
        while m <= 33:
            x = 100 - g - m
            if x % 3 == 0 and g * 5 + m * 3 + x // 3 == 100:
                print("公鸡: ", g, "只， 母鸡: ", m, "只, 小鸡: ", x, "只")
            m += 1
        g += 1
# }

#   umNum(endNum)
#   isYear(year)
#   countIsYear()
#   swapVar()
#   bitOp(num)
#   countBinOne(num)
#   ball()
#   buy()

#控制菜单：
while True:
    print("1. 输出1到100的和")
    print("2. 2000到2500之间闰年")
    print("3. 交换两个变量")
    print("4. 计算二进制位有多少个1")
    print("5. 位操作")
    print("6. 球的组合")
    print("7. 买鸡")
    print("8. 退出")
    sel = int(input("请输入[1 - 8]: "))
    print("-------------------------------------------")
    if sel == 1:
        sumNum(100)
    elif sel == 2:
        countIsYear()
    elif sel == 3:
        swapVar()
    elif sel == 4:
        countBinOne(int(input("please input num: ")))
    elif sel == 5:
        bitOp(int(input("please input num: ")))
    elif sel == 6:
        ball()
    elif sel == 7:
        buy()
    elif sel == 8:
        break
    print("-------------------------------------------")
